﻿using CCWin;
using SuperMap.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace MDF
{
    /// <summary>
    /// 点线数据集链接窗体
    /// </summary>
    public partial class TPPPLJ : CCSkinMain
    {
        private bool isCheck;
        private Datasources m_datasources;
        //xml读写类
        private IO_xml m_Xml;
        private string m_infoName;
        //记录信息
        private List<string> m_keys;
        private List<string> m_values;
        /// <summary>
        /// 链接数据集初始化
        /// </summary>
        /// <param name="datasources"></param>
        public TPPPLJ(Datasources datasources)
        {
            InitializeComponent();
            //非检查窗体
            isCheck = false;
            //获取数据源集合
            m_datasources = datasources;
            //实例化读写对象
            m_Xml = new IO_xml("PiPeiYuZhi");
            m_infoName = "TuoPuLink";
            //实例化
            m_keys = new List<string>();
            m_values = new List<string>();
            //填充树视图
            FillSets();
        }
        /// <summary>
        /// 变更点数据集名（检查用）
        /// </summary>
        /// <param name="setName"></param>
        public void ChangeCobomText(string setName)
        {
            try
            {
                //先清空再填充
                this.PointSet_Combo.Items.Clear();
                this.PointSet_Combo.Items.Add(setName);
                this.PointSet_Combo.SelectedIndex = 0;
                //检查窗体
                isCheck = true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 填充数据集
        /// </summary>
        private void FillSets()
        {
            try
            {
                foreach (Datasource datasource in m_datasources)
                {
                    //记录线数据集信息
                    TreeNode treeNode = new TreeNode() { Text = datasource.Alias };
                    foreach (Dataset dataset in datasource.Datasets)
                    {
                        if(dataset.Type == DatasetType.Line)
                        {
                            treeNode.Nodes.Add(dataset.Name);
                        }
                        else if (dataset.Type == DatasetType.Point)
                        {
                            this.PointSet_Combo.Items.Add(dataset.Name);
                        }
                    }
                    //如果该数据源有值，则添加到树视图
                    if (treeNode.Nodes.Count > 0)
                    {
                        this.LineSets_TreeView.Nodes.Add(treeNode);
                    }
                    if (this.PointSet_Combo.Items.Count > 0)
                    {
                        this.PointSet_Combo.SelectedIndex = 0;
                    }
                }
                //如果该树视图有值，则优化复选框
                if (this.LineSets_TreeView.Nodes.Count > 0)
                {
                    TreeNodeCheckBoxs();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选择节点时记录信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LineSets_TreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (e.Node.Checked)
                {
                    //选中时记录信息
                    m_values.Add(e.Node.Text);
                    m_keys.Add("LineSet" + m_values.Count.ToString());
                }
                else
                {
                    //取消选中时删除信息
                    int index = m_values.IndexOf(e.Node.Text);
                    m_values.Remove(e.Node.Text);
                    m_keys.RemoveAt(index);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 保存链接关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_keys != null && m_values != null && this.PointSet_Combo.Text != null)
                {
                    //保存结果
                    m_Xml.WriteXml(m_infoName, this.PointSet_Combo.Text, m_keys.ToArray(), m_values.ToArray());
                    MessageBox.Show("保存完成！","提示：");
                    if (isCheck)
                    {
                        //检查窗体返回OK
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void No_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (isCheck)
                {
                    //检查窗体返回OK
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    //退出
                    this.DialogResult = DialogResult.No;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        #region 隐藏根节点复选框
        /// <summary>
        /// 隐藏根节点复选框
        /// </summary>
        private void TreeNodeCheckBoxs()
        {
            try
            {
                this.LineSets_TreeView.CheckBoxes = true;
                this.LineSets_TreeView.ShowLines = true;
                this.LineSets_TreeView.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
                this.LineSets_TreeView.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(tvCheck_DrawNode);
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void tvCheck_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            try
            {
                if (e.Node.Level == 0)   //需要隐藏checkbox的节点级别
                {
                    HideCheckBox(this.LineSets_TreeView, e.Node);
                }

                e.DrawDefault = true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //一堆不懂de设置参数
        private const int TVIF_STATE = 0x8;
        private const int TVIS_STATEIMAGEMASK = 0xF000;
        private const int TV_FIRST = 0x1100;
        private const int TVM_SETITEM = TV_FIRST + 63;
        private void HideCheckBox(TreeView tvw, TreeNode node)
        {
            try
            {
                TVITEM tvi = new TVITEM();
                tvi.hItem = node.Handle;
                tvi.mask = TVIF_STATE;
                tvi.stateMask = TVIS_STATEIMAGEMASK;
                tvi.state = 0;
                SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
            }
            catch (Exception)
            {

                throw;
            }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
        private struct TVITEM
        {
            public int mask;
            public IntPtr hItem;
            public int state;
            public int stateMask;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszText;
            public int cchTextMax;
            public int iImage;
            public int iSelectedImage; public int cChildren; public IntPtr lParam;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TVITEM lParam);
        #endregion
        
    }
}
